
package XMLPack;

import Client.RequestI;
import XMLParser.MyXMLWriter;
import Client.Client;
import Graphic.MainForm;
import java.util.Arrays;
import java.util.Date;
import Data.Car;
import Data.Employee;
import java.util.ArrayList;

public class Request implements RequestI {

    private String fileName;
    private MyXMLWriter writer;
    private Client client= null;
    private MainForm form = null;

    public void setClient(Client cl){client = cl;}
    public Client getClient(){return client;}

    public String getTempName()
    {
     long unixTime = System.currentTimeMillis() / 1000L;
     System.out.println("temp\\"+unixTime+".xml");
     return "temp\\"+unixTime+".xml";

    }
    public Request(Request r)
    {
        client = r.getClient();
        fileName = r.fileName;
        writer = r.writer;
        form = r.form;
    }

    public Request(MainForm form, String serverAddress, int port, String _fileName)
    {
        client = Client.getInstance(form, serverAddress, port);
        fileName = _fileName;
        writer = new MyXMLWriter(fileName);
    }

    public String CheckUserPassword(String db, String db_password, String user, String pass)
    {
        String tempName = getTempName();
        writer.setFileName(tempName);
        String str[] = {"request","check_user", "db", "db_password","user","password"};
        String data[] = {"",db,db_password,user,pass};
        writer.SetTags(Arrays.asList(str));
        writer.Start();
        writer.Add(Arrays.asList(data));
        writer.Stop();
       return tempName;
    }

    public String citiesList(String db)
    {
        String [] str = {"request", "cities_list", "db"};
        String [] data = {"", db};
         String tempName = getTempName();
         writer.setFileName(tempName);
        writer.SetTags(Arrays.asList(str));
        writer.Start();
        writer.Add(Arrays.asList(data));
        writer.Stop();
         return tempName;
    }
    public String getKm(String db, String departure, String arrival)
    {
        String [] str = {"request", "get_km", "db", "departure", "arrival"};
        String [] data = {"", db, departure, arrival};
         String tempName = getTempName();
         writer.setFileName(tempName);
        writer.SetTags(Arrays.asList(str));
        writer.Start();
        writer.Add(Arrays.asList(data));
        writer.Stop();
         return tempName;
    }

    public String InsertEmployee(String db, String firstName,
            String lastName, String earning, String bonus, String workHour)
    {
        String str[] = {"request", "insert_employee", "db", "firstName", "lastName",
                            "earning","bonus", "workHours"};
        String data[] = {"",db, firstName, lastName, earning, bonus, workHour};
         String tempName = getTempName();
         writer.setFileName(tempName);
        writer.SetTags(Arrays.asList(str));
        writer.Start();
        writer.Add(Arrays.asList(data));
        writer.Stop();
        return tempName;
    }

    public String makeReservation(String db, String fname, String lname, String dataRezervarii, String plecare, String sosire, int pret){
        String str[] = {"request", "make_reservation", "db", "firstName","lastName", "dataRezervarii", "plecare","sosire","pret"};
        String data[] = {"",db, fname, lname, dataRezervarii,plecare,sosire, Integer.toString(pret)};
        System.out.println("Data rezervarii in request din client: "+ dataRezervarii.toString());
        String tempName = getTempName();
         writer.setFileName(tempName);
        writer.SetTags(Arrays.asList(str));
        writer.Start();
        writer.Add(Arrays.asList(data));
        writer.Stop();
         return tempName;
    }
    public String deleteReservation(String db, String fname, String lname, String dataRezervarii){
        System.out.println("Data rezervarii in request din Client: "+dataRezervarii);
        String str[] = {"request", "delete_reservation", "db", "firstName","lastName", "dataRezervarii"};
        String data[] = {"",db, fname, lname, dataRezervarii};
        System.out.println("Data rezervarii in request din client: "+ dataRezervarii.toString());
         String tempName = getTempName();
         writer.setFileName(tempName);
        writer.SetTags(Arrays.asList(str));
        writer.Start();
        writer.Add(Arrays.asList(data));
        writer.Stop();
         return tempName;
    }

     public String selectAll(String db, String tabel)
    {
        String [] str = {"request", "select_all", "db", "table"};
        String [] data = {"", db, tabel};
         String tempName = getTempName();
         writer.setFileName(tempName);
        writer.SetTags(Arrays.asList(str));
        writer.Start();
        writer.Add(Arrays.asList(data));
        writer.Stop();
        return tempName;
    }

     public String deleteCar(String db, String number)
     {
        String [] str = {"request", "delete_car", "db", "carNumber"};
        String [] data = {"", db, number};
         String tempName = getTempName();
         writer.setFileName(tempName);
        writer.SetTags(Arrays.asList(str));
        writer.Start();
        writer.Add(Arrays.asList(data));
        writer.Stop();
        return tempName;
     }
 public String selectCar(String db, String carNumber)
    {
         String [] str = {"request", "select_car", "db", "carNumber"};
        String [] data = {"", db, carNumber};
         String tempName = getTempName();
         writer.setFileName(tempName);
        writer.SetTags(Arrays.asList(str));
        writer.Start();
        writer.Add(Arrays.asList(data));
        writer.Stop();
         return tempName;
}
public String editCar(String db, Car car)
    {
         String [] str = {"edit_car", "car",  "db", "carId", "carNumber",
                "carModel", "seatsNumber","kmNumber", "maintenanceCost", "fuelConsumption"};
        Object [] data = {"", db, car.getCarId(), car.getNumber(), car.getModel(),
                             car.getSeatsNumber(), car.getKmNumber(),
                             car.getMaintenanceCost(), car.getFuelConsumption()};
        String tempName = getTempName();
        writer.setFileName(tempName);
        writer.SetTags(Arrays.asList(str));
        writer.Start();
        writer.Add(Arrays.asList(data));
        writer.Stop();
         return tempName;
    }
    public String insertCar(String db, Car car)
    {
         String [] str = {"insert_car", "car",  "db", "carId", "carNumber",
                "carModel", "seatsNumber","kmNumber", "maintenanceCost", "fuelConsumption"};
        Object [] data = {"", db, car.getCarId(), car.getNumber(), car.getModel(),
                             car.getSeatsNumber(), car.getKmNumber(),
                             car.getMaintenanceCost(), car.getFuelConsumption()};

         String tempName = getTempName();
         writer.setFileName(tempName);
        writer.SetTags(Arrays.asList(str));
        writer.Start();
        writer.Add(Arrays.asList(data));
        writer.Stop();
         return tempName;
    }

    public String editRoute(String db, String carNumber, String employeeLastName, String employeeFirstName, String dataCursei){
        String [] str = {"request", "edit_route", "db", "carNumber","lastName","firstName","dataCursei"};
        String [] data = {"", db,carNumber, employeeLastName, employeeFirstName, dataCursei};
         String tempName = getTempName();
         writer.setFileName(tempName);
        writer.SetTags(Arrays.asList(str));
        writer.Start();
        writer.Add(Arrays.asList(data));
        writer.Stop();
         return tempName;

    }
    public String newUser(String db, String fname, String lname, String km)
    {
        String [] str = {"request","new_user","db","firstName","lastName", "kmNumber"};
        String [] data = {"", db, fname, lname, km};
         String tempName = getTempName();
         writer.setFileName(tempName);
        writer.SetTags(Arrays.asList(str));
        writer.Start();
        writer.Add(Arrays.asList(data));
        writer.Stop();
         return tempName;


    }
    public String deleteUser(String db, String fname, String lname)
    {
        String [] str = {"request","delete_user","db","firstName","lastName"};
        String [] data = {"", db, fname, lname};
         String tempName = getTempName();
         writer.setFileName(tempName);
        writer.SetTags(Arrays.asList(str));
        writer.Start();
        writer.Add(Arrays.asList(data));
        writer.Stop();
         return tempName;
    }
    //afiseaza toate rezervarile pentru cursa de la o anumita data
    public String routeReport(String db, String dataCursei){
        String [] str = {"request","route_report","db","dataCursei"};
        String [] data = {"", db, dataCursei};
         String tempName = getTempName();
         writer.setFileName(tempName);
        writer.SetTags(Arrays.asList(str));
        writer.Start();
        writer.Add(Arrays.asList(data));
        writer.Stop();
         return tempName;
    }

    public String monthReport(String db, String startDate, String endDate){
        String [] str = {"request","month_report","db","startDate","endDate"};
        String [] data = {"", db, startDate, endDate};
         String tempName = getTempName();
         writer.setFileName(tempName);
        writer.SetTags(Arrays.asList(str));
        writer.Start();
        writer.Add(Arrays.asList(data));
        writer.Stop();
         return tempName;
    }

     public String totalMaintananceCost(String db){
        String [] str = {"request","total_cost","db"};
        String [] data = {"", db};
        String tempName = getTempName();
         writer.setFileName(tempName);
        writer.SetTags(Arrays.asList(str));
        writer.Start();
        writer.Add(Arrays.asList(data));
        writer.Stop();
        return tempName;
    }
      public String employeeSalary(String db){
        String [] str = {"request","employee_salary","db"};
        String [] data = {"", db};
         String tempName = getTempName();
         writer.setFileName(tempName);
        writer.SetTags(Arrays.asList(str));
        writer.Start();
        writer.Add(Arrays.asList(data));
        writer.Stop();
        return tempName;
    }

      public String deleteEmployee(String db, String fname, String lname)
      {
         
         // writer = new MyXMLWriter(tempName);
         System.out.println("*********DELTE EMPLOYEE in Request in client*************");
         System.out.println(fname+" "+lname);
         System.out.println("**********************");
         String [] str = {"request","delete_employee","db", "firstName", "lastName"};
         String [] data = {"", db, fname, lname};
         String tempName = getTempName();
         writer.setFileName(tempName);
         writer.SetTags(Arrays.asList(str));
         writer.Start();
         writer.Add(Arrays.asList(data));
         writer.Stop();
         writer.setFileName("localTemp.xml");
         return tempName;
      }
     public String editEmployees(String db, ArrayList<Employee> employees){
        String [] str = {"edit_employees","employee", "db","firstName",
        "lastName", "earning","workHours","bonus"};
       // employees.
         String tempName = getTempName();
         writer.setFileName(tempName);
        writer.SetTags(Arrays.asList(str));
        writer.Start();
        for(int i =0; i<employees.size(); i++){
            Employee e = employees.get(i);
            Object [] data = {"", db,e.getFirstName(), e.getLastName(), e.getEarning(),
                        e.getWorkHours(), e.getBonus()};
            writer.Add(Arrays.asList(data));
         }
        writer.Stop();
        return tempName;
    }


        public static void main(String args[]) throws InterruptedException
    {
        /*Request request = new Request("127.0.0.1", 8005, "localTemp.xml");
        request.CheckUserPassword("dmstc","","cri", "123");
        request.client.SetRequest("localTemp.xml");
        Thread.sleep(1000);
        String str = request.client.getParserAnswer();
        if (str.equalsIgnoreCase("true"))
            System.out.println("user and password correct"+str);
        else
            System.out.println("user and password don't match"+str);*/

    }

}
